<?php

/**
 * @url http://localhost/leetcode/leetcode-level.php
 * @since 2024-09-03 数据，https://github.com/stormsunshine/LeetCode-Levels
 * @since 2024-09-03 需要借助PhpSpreadsheet，改成手动处理6个txt文件
 */

$result = [];
for($txtNum=1; $txtNum<=6; $txtNum++){
    /**
     * @since 2024-09-03 读取文件，按行分隔
     */
    $filePath = "/tmp/leetcode/xls-{$txtNum}.txt";
    $lineArray = explode(PHP_EOL, file_get_contents($filePath));

    /**
     * @since 2024-10-09 首行抛弃
     */
    $i = 0;
    foreach ($lineArray as $lineString){
        if($i == 0){
            $i++;
            continue;
        } else {
            $i++;
        }
        /**
         * @since 2024-09-03 切割每一列数据
         */
        $dataArray = explode("	", $lineString);

        /**
         * @since 2024-09-03 提取url中的题目标题TitleSlug
         */
        switch ($txtNum){
            case 1:
                $urlIndex = 3;
                $levelIndex = 6;
                break;
            case 2:
            case 3:
                $urlIndex = 2;
                $levelIndex = 5;
                break;
            case 4:
            case 5:
            case 6:
                $urlIndex = 2;
                $levelIndex = 3;
                break;
        }

        $url = $dataArray[$urlIndex];
        $level = $dataArray[$levelIndex];
        $titleSlug = str_replace(["https://leetcode.cn/problems/", "/"], "", $url);
        $result[$titleSlug] = $level;
    }
}

/**
 * @since 2024-12-05 输出数据量
 */
$list = [
    "rows" => count($result),
    "data" => $result,
];
echo json_encode($list, JSON_UNESCAPED_UNICODE);
file_put_contents("/tmp/leetcode/level.json", json_encode($result, JSON_UNESCAPED_UNICODE));
